#ifndef PURE_OBJECT_H_
#define PURE_OBJECT_H_

// TODO: Move this into a config header.
#define __STDC_LIMIT_MACROS
#include <stdint.h>
#include <assert.h>

namespace Pure
{
    class Object
    {
    public:
        Object(): mRefCount(1) {}
        ~Object() { assert(mRefCount == 1); }
        
        /**
         * Increase the reference count only if the reference count is already
         * greater than 0.
         */
        void ref() 
        { 
            assert(mRefCount > 0 && mRefCount <= INT32_MAX); 
            mRefCount++; 
        }
        
        /**
         * Decrease the reference count, delete ourself when the reference count is 0.
         */
        void unref() 
        { 
            assert(mRefCount > 0 && mRefCount <= INT32_MAX);
            if (mRefCount == 1)
                delete this;
            mRefCount--;
        }
        
        /**
         * Safe way to call ref in case the object is NULL.
         */
        void safeRef()
        {
            if (this)
                ref();
        }
        
        /**
         * Safe was to call unref in case the object is NULL.
         */
        void safeUnref()
        {
            if (this)
                unref();
        }
        
    private:
        int32_t mRefCount;
    };
    
} // end namespace Pure

#endif /*PURE_OBJECT_H_*/
